package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.label.pojo.dto.LabelGroupDeleteDTO;
import com.xbongbong.pro.label.pojo.dto.LabelGroupListDTO;
import com.xbongbong.pro.label.pojo.dto.LabelGroupRepeatDTO;
import com.xbongbong.pro.label.pojo.dto.LabelGroupSaveDTO;
import com.xbongbong.pro.label.pojo.dto.LabelGroupSortDTO;
import com.xbongbong.pro.label.pojo.vo.LabelGroupDeleteVO;
import com.xbongbong.pro.label.pojo.vo.LabelGroupListVO;
import com.xbongbong.pro.label.pojo.vo.LabelGroupRepeatVO;
import com.xbongbong.pro.label.pojo.vo.LabelGroupSaveVO;
import com.xbongbong.pro.label.pojo.vo.LabelGroupSortVO;
import com.xbongbong.saas.service.LabelGroupService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author guohui.zhang
 * @Title: 标签设置相关controller
 * @date 2020/11/39:16
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/labelGroup/set")
public class LabelGroupSettingController {

    @Resource
    private LabelGroupService labelGroupService;

    /**
     * 标签分组列表
     * @param labelGroupListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupList(@RequestBody @Valid LabelGroupListDTO labelGroupListDTO, BindingResult br) throws Exception {
        XbbResponse<LabelGroupListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelGroupListVO workOrderTemplateListVO = labelGroupService.getGroupList(labelGroupListDTO);
                response = new XbbResponse<>(workOrderTemplateListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 标签分组保存
     * @param labelGroupSaveDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateList(@RequestBody @Valid LabelGroupSaveDTO labelGroupSaveDTO, BindingResult br) throws Exception {
        XbbResponse<LabelGroupSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelGroupSaveVO labelGroupSaveVO = labelGroupService.saveLabelGroup(labelGroupSaveDTO);
                response = new XbbResponse<>(labelGroupSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 标签分组删除
     * @param labelGroupDeleteDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid LabelGroupDeleteDTO labelGroupDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<LabelGroupDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelGroupDeleteVO labelGroupDeleteVO = labelGroupService.deleteLabelGroup(labelGroupDeleteDTO);
                response = new XbbResponse<>(labelGroupDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 分组排序
     * @param labelGroupSortDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid LabelGroupSortDTO labelGroupSortDTO, BindingResult br) throws Exception {
        XbbResponse<LabelGroupSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelGroupSortVO labelGroupSortVO = labelGroupService.sortLabelGroup(labelGroupSortDTO);
                response = new XbbResponse<>(labelGroupSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 标签分组是否存在
     * @param labelGroupRepeatDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/repeat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String repeat(@RequestBody @Valid LabelGroupRepeatDTO labelGroupRepeatDTO, BindingResult br) throws Exception {
        XbbResponse<LabelGroupRepeatVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelGroupRepeatVO labelGroupRepeatVO = labelGroupService.repeatGroup(labelGroupRepeatDTO);
                response = new XbbResponse<>(labelGroupRepeatVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
